// 装饰器模式：日志记录
export function withLogging(target, name, descriptor) {
  const original = descriptor.value
  
  if (typeof original === 'function') {
    descriptor.value = function(...args) {
      console.log(`调用方法: ${name}`, args)
      try {
        const result = original.apply(this, args)
        console.log(`方法 ${name} 执行成功`, result)
        return result
      } catch (error) {
        console.error(`方法 ${name} 执行失败`, error)
        throw error
      }
    }
  }
  
  return descriptor
}

// 装饰器模式：性能监控
export function withPerformance(target, name, descriptor) {
  const original = descriptor.value
  
  if (typeof original === 'function') {
    descriptor.value = function(...args) {
      const start = Date.now()
      const result = original.apply(this, args)
      const end = Date.now()
      
      console.log(`方法 ${name} 执行耗时: ${end - start}ms`)
      return result
    }
  }
  
  return descriptor
}

// 装饰器模式：缓存
export function withCache(target, name, descriptor) {
  const original = descriptor.value
  const cache = new Map()
  
  if (typeof original === 'function') {
    descriptor.value = function(...args) {
      const key = JSON.stringify(args)
      
      if (cache.has(key)) {
        console.log(`缓存命中: ${name}`, args)
        return cache.get(key)
      }
      
      const result = original.apply(this, args)
      cache.set(key, result)
      
      return result
    }
  }
  
  return descriptor
}